﻿using System;
using System.Data;
using ASPNETVN.PORTAL.Components.Helpers;
using ASPNETVN.PORTAL.Components.UI;
using ASPNETVN.PORTAL.Modules.DVDZone.Components;

namespace ASPNETVN.PORTAL.Modules.DVDZone.Controls
{
    public partial class ItemDetail : ModulePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                Components.Item item = Components.Item.Load(this.ItemID);
                if (item != null)
                {
                    // Build page link.
                    PageLink pagelink = (PageLink)this.ParentControl.FindControl("MainPageLink");

                    int categoryID = item.CategoryID;
                    Category category = Category.Load(categoryID);
                    Category parentCategory = Category.Load(category.ParentID);

                    pagelink.AddLink(parentCategory.Title, "#");

                    string url = ResolveClientUrl(HttpUrlBuilder.BuildUrl(this.ModuleID, 0, ModulePages.Item, "categoryid={0}", category.ID));
                    pagelink.AddLink(category.Title, url);

                    pagelink.AddLink(item.Title, "#");


                    btnScreenshot.NavigateUrl = item.ImagePath;
                    lblTrailerTitle.Text = item.Title;

                    this.imgPicture.ImageUrl = item.ImagePath;
                    this.lblTitle.Text = item.Title;
                    //lblPublishedTime.Text = item.PublishedTime.ToLongDateString() + " - " + item.Authors;
                    //lblTotalViews.Text = item.TotalViews.ToString("N0");
                    this.ltrRate.Text = ImageHelper.BuildRateImage(item.Rate, this.Request);
                    this.lblContent.Text = this.Server.HtmlDecode(item.Memo);

                    // Screenshot.
                    DataSet ds = Media.SelectBy_ItemID(this.ItemID);
                    
                    DataView dvScreenshot = ds.Tables[0].DefaultView;
                    dvScreenshot.RowFilter = "MediaType = 'Picture'";
                    /*
                    DataRow[] rows = ds.Tables[0].Select("MediaType = 'Picture'");

                    if (rows.Length > 0)
                    {
                        btnScreenshot.NavigateUrl = rows[0]["FilePath"].ToString();
                    }
                    */
                    rptScreenshot.DataSource = dvScreenshot;
                    rptScreenshot.DataBind();

                    // Trailer.
                    DataRow[] rows = ds.Tables[0].Select("MediaType = 'Movie'");
                    if (rows.Length > 0)
                    {
                        literalTrailer.Text = rows[0]["FilePath"].ToString();
                    }
                    btnTrailer.Enabled = rows.Length > 0;
                }
            }
        }
    }
}